#ifndef __SMART_PTR__
#define __SMART_PTR__
#include<iostream>
using namespace std;

template<typename VAR>
class smart_ptr{
public:
    smart_ptr(): p(nullptr), use(new int(1)){}
    smart_ptr(const VAR& var): p(new VAR(var)), use(new int(1)){}
    smart_ptr(const smart_ptr& other): p(other.p), use(other.use){*use ++;}
    ~smart_ptr(){decr_use();}

    smart_ptr& operator=(const smart_ptr& other){
        *other.use++;
        decr_use();
        p = other.p;
        use = other.use;
        return *this;
    }

    const VAR* operator-> (){if(p) return p;}
    const VAR& operator* (){if(p) return *p;}

private:
    VAR* p;
    int* use;   //用来对指向同一个数据的所有智能指针类计数，所以必须用指针
    void decr_use(){ 
        if(--(*use) == 0){
            delete p;
            delete use;
        }   
    }
};


#endif